package Hash;

import java.util.Arrays;

public class HashBuck {
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node[]array=new Node[10];
    public int useSize;
    public static final double DEFAULT_LOAD_FACTOR=0.75;


    public void put(int key,int val){
int k=key%array.length;
Node cur=array[k];
while(cur!=null){
    if(cur.key==key){
        cur.val=val;
        return;
    }
    cur=cur.next;
}
Node node=new Node(key, val);
node.next=array[k];
array[k]=node;
useSize++;
if(doLodeFactor()>=DEFAULT_LOAD_FACTOR){
    reSize();
        }
    }

private double doLodeFactor(){
        return useSize*1.0/array.length;
}

private void reSize(){
Node[]newArray= new Node[array.length*2];
    for (int i = 0; i <array.length ; i++) {
        Node cur=array[i];
        while(cur!=null){
            int k= cur.key% newArray.length;
            Node curN=cur.next;
cur.next=newArray[k];
            newArray[k]=cur;
            cur=curN;
        }
    }
    array=newArray;
}

public int getvalues(int key){
Node cur=array[key% array.length];
        while(cur!=null){
         if (cur.key==key)
            return cur.val;
         cur=cur.next;
        }

    return -1;
}



}
